var temparr = new Array(2, 4);
var bc0 = document.getElementsByClassName("bc0");
var arr;
var arrback = new Array();
for(var i = 0; i < 3; i++){
    arrback[i] = new Array();
    for(var j = 0; j < 4; j++){
        arrback[i][j] = new Array();
    for(var k = 0; k < 4; k++){
        arrback[i][j][k]=0;
        }
    }
}
//撤销
function reback() {
    var span=document.getElementById("time");
    var btn_back=document.getElementById("btn_back");
    var time=span.innerHTML;
    console.log(time);
    time-=1;
    span.innerHTML=time;
    if(time==0){
        btn_back.disabled=true;
        btn_back.style.backgroundColor="gray";
    }

}
//重新开始
function reset() {
    arr = new Array([0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]);
    for (var i = 0; i <= 3; i++) {
        arr[Math.floor(Math.random() * 4)][Math.floor(Math.random() * 4)] = temparr[Math.round(Math.random())];
    }
    init();

}
function init() {
    var i = 0, j, k;
    //初始化元素
    for (j = 0; j < 4; j++) {
        for (k = 0; k < 4; k++) {
            bc0[i].innerHTML = arr[j][k];
            i++;
        }
    }
    //记录元素
    for(var i=0;i<=3;i++){
        for(var j=0;j<3;j++){
            arrback[2][i][j]=arrback[1][i][j];
            arrback[1][i][j]=arrback[0][i][j];;
            arrback[0][i][j]=arr[i][j];
        }
    }
    // var temp1=arrback[1];
    // arrback[1]=temp0;
    // arrback[2]=temp1;
    console.log(arrback);
    // if (arrback.length >= 3) {
    //     arrback = arrback.slice(1);
    // }
    // var t = arr.concat();
    // arrback.push(t);

    //根据div内容判断div背景颜色
    for (var i = 0; i < bc0.length; i++) {
        var n = bc0[i].innerHTML.toString();
        switch (n) {
            case '0':
                bc0[i].style.background = "#ccc0b2";
                bc0[i].style.fontSize = "50px";
                break;
            case '2':
                bc0[i].style.background = '#FFE1FF';
                bc0[i].style.fontSize = "50px";
                break;
            case '4':
                bc0[i].style.background = "#FFDAB9"
                bc0[i].style.fontSize = "50px";
                break;
            case '8':
                bc0[i].style.background = "#FFE4B5";
                bc0[i].style.fontSize = "50px";
                break;
            case '16':
                bc0[i].style.background = "#FFD39B";
                bc0[i].style.fontSize = "50px";
                break;
            case '32':
                bc0[i].style.background = "#FFA07A";
                bc0[i].style.fontSize = "50px";
                break;
            case '64':
                bc0[i].style.background = "#CDAD00";
                bc0[i].style.fontSize = "50px";
                break;
            case '128':
                bc0[i].style.background = "#ffd09f";
                bc0[i].style.fontSize = "40px";
                break;
            case '256':
                bc0[i].style.background = "#FFB90F";
                bc0[i].style.fontSize = "40px";
                break;
            case '512':
                bc0[i].style.background = "#FFA54F";
                bc0[i].style.fontSize = "40px";
                break;
            case '1024':
                bc0[i].style.background = "	#FF8C00";
                bc0[i].style.fontSize = "35px";
                break;
            case '2048':
                bc0[i].style.background = "#FF7F50";
                bc0[i].style.fontSize = "35px";
                break;
            case '4096':
                bc0[i].style.background = "#FF4500";
                bc0[i].style.fontSize = "35px";
                break;
            case '8192':
                bc0[i].style.background = "#FF3030";
                bc0[i].style.fontSize = "35px";
                break;
        }
    }

}


function addNewNumber() {
    var temp2 = new Array();
    for (var i = 0; i < 4; i++) {
        for (var j = 3; j >= 0; j--) {
            if (arr[i][j] == 0) {
                temp2.push(i + "" + j);
            }
        }
    }
    var position = temp2[Math.floor(temp2.length * Math.random())].split("");
    arr[position[0]][position[1]] = temparr[Math.round(Math.random())];
    init();
}

//通过键盘来控制移动
document.onkeydown = function (event) {
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if (e && e.keyCode == 37) { // 左 键
        var flag = false;
        for (i = 0; i < 4; i++) {
            //合并
            for (j = 0; j < 4; j++) {
                for (var k = 1; k <= 3 - j; k++) {
                    switch (k) {
                        case 1:
                            if (arr[i][j] == arr[i][j + k] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i][j + k] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }
                        case 2:
                            if (arr[i][j + 1] == 0 && arr[i][j] == arr[i][j + k] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i][j + k] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }

                        case 3:
                            if (arr[i][+1] == 0 && arr[i][j + 2] == 0 && arr[i][j] == arr[i][j + k] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i][j + k] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }
                    }
                    break;
                }

            }

            for (var j = 0; j < 3; j++) {
                if (arr[i][j] == 0) {
                    for (var k = 1; k < 4 - j; k++) {
                        if (arr[i][j + k] != 0) {
                            arr[i][j] = arr[i][j + k];
                            arr[i][j + k] = 0;
                            flag = true;
                            break;
                        }
                    }
                }
            }
        }
        if (flag) {
            //随机新增数值
            addNewNumber();
        }

    }

    if (e && e.keyCode == 38) { // 上 键
        var flag = false;
        for (j = 0; j < 4; j++) {
            //合并
            for (i = 0; i < 4; i++) {
                for (var k = 1; k <= 3 - i; k++) {
                    switch (k) {
                        case 1:
                            if (arr[i][j] == arr[i + k][j] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i + k][j] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }
                        case 2:
                            if (arr[i + 1][j] == 0 && arr[i][j] == arr[i + k][j] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i][j + k] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }

                        case 3:
                            if (arr[i + 1][j] == 0 && arr[i + 2][j] == 0 && arr[i][j] == arr[i + k][j] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i + k][j] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }
                    }
                    break;
                }

            }
            //移动
            for (var i = 0; i < 3; i++) {
                if (arr[i][j] == 0) {
                    for (var k = 1; k < 4 - i; k++) {
                        if (arr[i + k][j] != 0) {
                            arr[i][j] = arr[i + k][j];
                            arr[i + k][j] = 0;
                            flag = true;
                            break;
                        }
                    }
                }
            }

        }
        if (flag) addNewNumber();
    }


    if (e && e.keyCode == 39) { // 右 键
        var flag = false;
        for (i = 0; i < 4; i++) {
            //合并
            for (j = 3; j >= 0; j--) {
                for (var k = 1; k <= j; k++) {
                    switch (k) {
                        case 1:
                            if (arr[i][j] == arr[i][j - k] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i][j - k] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }
                        case 2:
                            if (arr[i][j - 1] == 0 && arr[i][j] == arr[i][j - k] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i][j - k] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }

                        case 3:
                            if (arr[i][j - 1] == 0 && arr[i][j - 2] == 0 && arr[i][j] == arr[i][j - k] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i][j - k] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }
                    }
                    break;

                }

            }
            for (j = 3; j >= 0; j--) {
                if (arr[i][j] == 0) {
                    for (var k = 0; k <= j; k++) {
                        if (arr[i][j - k] != 0) {
                            arr[i][j] = arr[i][j - k];
                            arr[i][j - k] = 0;
                            flag = true;
                            break;
                        }
                    }
                }
            }

        }
        if (flag) addNewNumber();

    }

    if (e && e.keyCode == 40) { // 下键
        var flag = false;
        for (j = 0; j < 4; j++) {
            //合并
            for (i = 3; i >= 0; i--) {
                for (var k = 1; k <= i; k++) {
                    switch (k) {
                        case 1:
                            if (arr[i][j] == arr[i - k][j] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i - k][j] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }

                        case 2:
                            if (arr[i - 1][j] == 0 && arr[i][j] == arr[i - k][j] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i - k][j] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }


                        case 3:
                            if (arr[i - 1][j] == 0 && arr[i - 2][j] == 0 && arr[i][j] == arr[i - k][j] && arr[i][j] != 0) {
                                arr[i][j] = arr[i][j] * 2;
                                arr[i - k][j] = 0;
                                flag = true;
                                break;
                            } else {
                                continue;
                            }
                    }
                    break;

                }

            }
            //移动
            for (var i = 3; i >= 0; i--) {
                if (arr[i][j] == 0) {
                    for (var k = 1; k <= i; k++) {
                        if (arr[i - k][j] != 0) {
                            arr[i][j] = arr[i - k][j];
                            arr[i - k][j] = 0;
                            flag = true;
                            break;
                        }
                    }
                }
            }
        }
        if (flag) addNewNumber();
    }
}